package com.dahuan.producer;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class CallbackProducer {
    public static void main(String[] args) {
        //TODO 创建配置信息
        Properties properties = new Properties();
        properties.put( ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux-1:9092" );
        properties.put( ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer" );
        properties.put( ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer" );

        //TODO 创建生产者对象
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>( properties );

        for (int i = 0; i < 10; i++) {
            producer.send( new ProducerRecord<String, String>( "first","dahuan"+i ), new Callback() {

                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    if (exception == null ){
                        System.out.println(metadata.partition() + "--" +metadata.offset());
                    }else {
                        exception.printStackTrace();
                    }
                }
            } );
        }
        //TODO 关闭资源
        producer.close();
    }
}
